package com.maomao.register;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.SQLException;
import java.util.Date;
import java.util.Properties;

import javax.mail.Address;
import javax.mail.Authenticator;
import javax.mail.Message.RecipientType;
import javax.mail.MessagingException;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.maomao.db.*;

public class ConfirmServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		String toMail = request.getParameter("email");
		String registerName = request.getParameter("userName");
		String studentId = request.getParameter("stuId");
		String passwd = request.getParameter("passwd1");
		String temp = request.getParameter("userType");
		int userType = Integer.parseInt(temp);
		
		response.setContentType("text/html;charset=utf-8");
		PrintWriter out = response.getWriter();
		
		String sql="select * from person where ID = '"+studentId+"'";
		System.out.println(sql);
		
		DbConnection db = new DbConnection();
		
		int size=0;
		
		try {
			size = db.getSzie(sql);
			db.close();
//			System.out.println(size);
		} catch (SQLException e2) {
			// TODO Auto-generated catch block
			e2.printStackTrace();
		}
		
		if(size>0)
		{
			sql =  "select * from person where ID = '"+studentId+"' and isChecked = 0";
			
			try {
				size = db.getSzie(sql);
				db.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
			if(size>0)
				out.println("<script language='javascript'>alert('注册成功，尚未验证'); location='login.jsp';</script>");
			else
				out.println("<script language='javascript'>alert('尚未注册'); location='login.jsp';</script>");
		}
		else
		{
		
			sql = "insert into person values ('"+studentId+"','"+registerName+"','"+toMail+"','"+passwd+"',"+"0,"+userType+")";
			
			try {
				db.update(sql);
			} catch (ClassNotFoundException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			} catch (SQLException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
			try {
				db.close();
			} catch (SQLException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
			
			
			String userName = "yzt112233@163.com";    	//锟斤拷锟斤拷锟街�
			String password = "yuzhenting";	 	//锟斤拷锟斤拷锟斤拷锟斤拷
			
			String registerId = "" + Math.random() * Math.random();
			String url = "http://localhost:8080/test/ProcessMailServlet?registerId=" + registerId;//锟斤拷锟斤拷没锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷械锟斤拷锟斤拷锟斤拷锟斤拷踊氐锟斤拷锟斤拷锟斤拷站锟斤拷
			
			HttpSession httpSession = request.getSession();
			httpSession.setAttribute(registerId, studentId);
			httpSession.setMaxInactiveInterval(600);
			
			Properties props = new Properties();
			props.setProperty("mail.smtp.host", "smtp.163.com");
			props.setProperty("mail.smtp.auth", "true");
			
			Authenticator authenticator = new MyAuthenticator(userName, password);
			
			javax.mail.Session session = javax.mail.Session.getInstance(props,authenticator);
			session.setDebug(true);
			
			try{
				Address from = new InternetAddress(userName);
				Address to = new InternetAddress(toMail);
				
				MimeMessage msg = new MimeMessage(session);
				msg.setFrom(from);
				msg.setSubject("欢迎注册");  
		        msg.setSentDate(new Date());  
		        msg.setContent("<a href='" + url + "'>点击" + url + "完成注册</a>", "text/html;charset=utf-8");  
				/*
				Transport transport = session.getTransport("smtp");
				transport.connect("smtp.163.com", userName, password);
				transport.sendMessage(msg,msg.getAllRecipients());
				transport.close();
				*/
				Transport.send(msg);
			} catch(MessagingException e){
				e.printStackTrace();
			}
			
			out.println("<script language='javascript'>alert('注册成功请去邮箱验证'); location='login.html';</script>");
		}
	}

	private String String(byte[] bytes, String string) {
		// TODO Auto-generated method stub
		return null;
	}

	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		doGet(request, response);
	}
}